<?php
namespace Home\Controller;
use Think\Controller;
use Home\Aip\Store;


class CommonController extends Controller{
	/*初始化
	* 构造函数
	*/
    public function _initialize(){
		
		//全部小写
		$rule = array('login'=>array('login','getout','shop_reg','userlogin','loginon','reg','forget_password','verify','check_verify'),'shop'=>array('index','sp_storein','code_chage'),'error'=>array('error'));
		 
		$url = in_array(strtolower(ACTION_NAME),$rule[strtolower(CONTROLLER_NAME)]);
		$array_key = array_key_exists(strtolower(CONTROLLER_NAME),$rule);
		//首页中部广告
		$adv_h0 = S('home_adv_0');
		
		//超时强制退出
		$timeover = timeOver();

       if ($array_key && $url) {
		//
       } else {
			//全部小写
		   if (CONTROLLER_NAME!=='Index' && CONTROLLER_NAME!=='SpGoods' && CONTROLLER_NAME!=='SpCart') {
			   if(!session('boboid') && !$url){				   
				  $this->redirect('/login','请先登录！');
			   } else {			   
					if (!$timeover && $url) {					
						//$this->redirect('/login','登录超时，请重新登录！');
					}			   
			   }			   
		   }							
       }
		
		$stc = new Store();				
		$store_class = $stc ->StoreClass();		
				
		//需要会员登录
	   if (session('boboid') && session('user_name')) {
			//会员信息
			if ($_SESSION['user_id'] && session('boboid')) {
				unset($str);
				unset($data);
				unset($cache);
				$str = "id=%d and username='%s'";
				$data = array($_SESSION['user_id'],$_SESSION['user_name']);			
				$cache = 'user_'.$_SESSION['user_id'];
				//S($cache,null);	
				$join = 'RIGHT JOIN __INTEGRAL__ ON __INTEGRAL__.userid = __USER__.id';				
				//关联查询
				$UserInfo = _getJoinOne('user',$join,$str,$data,$cache);					
			}
			
		   //店铺入驻申请页	
			$joinin = array('shop'=>array('sp_sep1_c2c','sp_sep2_c2c','sp_sep3_c2c','sp_sep4_c2c','sp_sep5_c2c'));
			
			$st_url = in_array(ACTION_NAME,$joinin[strtolower(CONTROLLER_NAME)]);
			$sp_url = C('CONTROLLER_NAME');
			
			if ($st_url || CONTROLLER_NAME=='SpStore') {
				
				$stc = new Store();	
				$uid = $_SESSION['user_id'] ;
				
				//更新数据时及时更新session
				unset($info);
				$info = 'st_joinin_data' ;					
				userInfo_find($uid,$info) ;
/* 				session('st_joinin_data',null);
				session('shop_store',null); */
				//更新数据时及时更新session					
				unset($info);
				$info = 'shop_store' ;
				userInfo_find($uid,$info) ; 					
				
				if (!$_SESSION['st_joinin_data']) {
					unset($str);
					unset($data);
					$str = "member_id=%d and member_name='%s' and joinin_state>%d";
					$data = array($_SESSION['user_id'],$_SESSION['user_name'],0) ;
					$store_ff = _getOne('store_joinin',$str,$data) ;
					session('st_joinin_data',$store_ff);									
				} else {
					$store_ff = $_SESSION['st_joinin_data'] ;
				}
				
				if (!$_SESSION['shop_store']) {
					unset($str);
					unset($data);
					$str = "member_id=%d and member_name='%s'";
					$data = array($_SESSION['user_id'],$_SESSION['user_name']) ;			
					$store_ff2 = _getOne('store',$str,$data) ;
/* 					dump($data);
					dump($store_ff2);die; */
					//店铺等级
					unset($str);
					unset($data);
					$str = "sg_id=%d";
					$data = array($store_ff2['grade_id']) ;		
					$st_grade = _getOne('store_grade',$str,$data) ;
					$st_grade && $store_ff2['sg_name'] = $st_grade['sg_name'] ;
					$st_grade && session('shop_store',$store_ff2);	
					
				} else {
					$store_ff2 = $_SESSION['shop_store'] ;
				}				
				
				if ($store_ff || $store_ff2['store_state']=='2') {
					//已经拥有店铺或者已经申请审核的店铺
					$jump = 1 ;										
				} else {
					$jump = 0 ;
				}
				
				//登录限制 (商家中心/店铺)		
				if ($store_ff2['store_id'] && CONTROLLER_NAME=='SpStore' && $store_ff2['store_name']) {
					
					if ($store_ff2['store_state']=='0') {
						$this->error('店铺已关闭','/shop/index');
					} else if ($store_ff2['store_state']=='2') {
						$this->error('店铺审核中','/shop/index');
					}else {
						unset($str);
						unset($data);
						//只能用session
						if (!$_SESSION['st_sg_name']) {
							$str = "sg_id=%d";
							$data = array($store_ff2['grade_id']) ;
							$grade_ff = _getOne('store_grade',$str,$data) ;
							$store_ff2 ['grade_name'] = $grade_ff ['sg_name'];
							session('st_sg_name',$grade_ff ['sg_name']) ;							
						} else {
							$store_ff2 ['grade_name'] = $_SESSION['st_sg_name'];
						} 
						
						if ($store_ff2 ['store_end_time']) {
							$store_ff2 ['store_end_time'] = date('Y-m-d',$store_ff2 ['store_end_time']) ;
						} else {
							$store_ff2 ['store_end_time'] = '不限制' ;
						}	
						//店铺左边导航（暂时）
						if (ACTION_NAME=='st_goods_list') {
							$st_css = 1 ;
							
						} else if (ACTION_NAME=='st_depot_list' || ACTION_NAME=='st_gotout_list' || ACTION_NAME=='st_goods_await_list') {
							$st_css = 2 ;
						}
						
						if (ACTION_NAME=='st_store_setting') {							
							$st_css = 1 ;							
						} else if (ACTION_NAME=='st_store_datum' || ACTION_NAME=='st_gotout_list' || ACTION_NAME=='st_goods_await_list') {
							$st_css = 2 ;							
						}
						//商家中心 number_format($str,2)			
					   				
						$this->assign('st_css',$st_css);											
						$this->assign('store',$store_ff2);//会员店铺信息
					}			
				} else if (!$store_ff2['store_id'] && CONTROLLER_NAME=='SpStore') {
					$this->redirect('/store_joinin');
				} 
				
				$this->assign('joinin',$store_ff);	 				
			}
		    
			//店铺入驻审核状况	
			if ($store_ff['joinin_state']=='20' || $store_ff['joinin_state']=='31' || $store_ff['joinin_state']=='11' || $store_ff['joinin_state']=='40' || $store_ff2['store_state']=='1') {				
				$jump = 2 ;
			}
			
			if (CONTROLLER_NAME == 'Shop') {
								
				if ($jump == 2  && ACTION_NAME!=='sp_sep5_c2c') {
					if ($_SESSION['shop_store']['is_own_shop']=='1') {
						$this->redirect('/store/index');
					} else {
						$this->redirect('/store_joinin/c2c_step5');
					}					
					 
				} else if ($jump == 1 && ACTION_NAME!=='sp_sep4_c2c') {				
					$this->redirect('/store_joinin/c2c_step4');
				} 				
			}
			//dump($UserInfo);die;
		   $this->assign('UserInfo',$UserInfo);//会员信息
	   }
	   
	   //不需要登录	   
	   //平台客服	   
		unset($str);
		unset($data);
		unset($cache);
		$str = "name<>'%s'";
		$data = array('');	
		$cache = 'service_info' ;
		$service = _getList('shop_setting',$str,$data,$cache);
		//S($cache,null) ;		
		//省市
		$cache2 = 'province';
		$code = code_area($cache2);
		
		//购物中商品数量
		if ($_SESSION['user_id'] && $_SESSION['boboid']==$_SESSION['user_id']) {		
			$c_gnum = session('_cart_goods_num') ;
			!$c_gnum  && $c_gnum = cookie('_cart_goods_num') ;	
		} else {			
			$c_gnum = cookie('_cart_goods_num') ;			
		}
 		//分区			 
		$this-> getNavArea();	
		$this->assign('cart_goods_num',$c_gnum);	
		//dump($store_class);die;
		$this->assign('sc_class',$store_class);//商品分类
		$this->assign('code',$code);
		$this->assign('service',$service);
	    $this->assign('username',session('user_name'));	
		$this->assign('adv_h0',$adv_h0) ;//首页顶部广告

    }
	
    public function uploadFace() {
	
		$upload = $this->_upload('Pic');
		// dump($upload);die;
		$this->ajaxReturn($upload);
	}
	
	Private function _upload ($path) {
		import('ORG.Net.UploadFile');	//引入ThinkPHP文件上传类
		$obj = new \Think\Upload();	//实例化上传类
		$obj->maxSize = 2000000;	//图片最大上传大小
		$obj->savePath =  $path . '/';	//图片保存路径
		$obj->saveRule = 'uniqid';	//保存文件名
		$obj->uploadReplace = true;	//覆盖同名文件
		$obj->allowExts = array('jpg','jpeg','png','gif');	//允许上传文件的后缀名
	
		$obj->autoSub = true;	//使用子目录保存文件
		$obj->subType = 'date';	//使用日期为子目录名称
		$obj->dateFormat = 'Y_m';	//使用 年_月 形式
		//$obj->upload();die;
		$info   =   $obj->upload();
		if (!$info) {
			return array('status' => 0, 'msg' => $obj->getErrorMsg());
		} else {
			foreach($info as $file){
				$pic = $file['savepath'].$file['savename'];
			}
			//$pic =  $info[0][savename];
			//echo $pic;die;
			return array(
					'status' => 1,
					'path' => $pic
			);
		}
	}
	
	/*
	* 获取分区
	*/
	Private function getNavArea() {
		$shop_area = M('shop_area') -> where('shop_area_state = 1') -> order('shop_area_sort ASC') -> select() ;
		//dump($shop_area);die;
		$this->assign('area_list', $shop_area);
		return true;
	}

}